﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TicketBin.cs" company="Mar3ek">
//   http://mar3ek.wordpress.com
// </copyright>
// <summary>
//   Class representing a ticket bin
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace LighthouseNET
{
    using System;
    using System.Xml.Linq;

    /// <summary>
    /// Class representing a ticket bin
    /// </summary>
    public class TicketBin
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TicketBin"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="query">The query.</param>
        /// <param name="isDefault">Whether this ticket bin should become a default ticket bin.</param>
        public TicketBin(string name, string query, bool isDefault)
        {
            Name = name;
            Query = query;
            IsDefault = isDefault;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TicketBin"/> class. 
        /// </summary>
        /// <param name="isDefault">
        /// if set to <c>true</c> [is default].
        /// </param>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="projectId">
        /// The project id.
        /// </param>
        /// <param name="query">
        /// The query.
        /// </param>
        /// <param name="ticketsCount">
        /// The tickets count.
        /// </param>
        /// <param name="updatedAt">
        /// The updated at.
        /// </param>
        /// <param name="userId">
        /// The user id.
        /// </param>
        private TicketBin(
            bool isDefault,
            int id,
            string name,
            int projectId,
            string query,
            int ticketsCount,
            DateTime updatedAt,
            int userId)
        {
            Name = name;
            Query = query;
            IsDefault = isDefault;
            Id = id;
            ProjectId = projectId;
            TicketsCount = ticketsCount;
            UpdatedAt = updatedAt;
            UserId = userId;
        }

        #region Properties
        /// <summary>
        /// Gets the user id.
        /// </summary>
        public int UserId { get; private set; }

        /// <summary>
        /// Gets a value indicating whether this instance is default.
        /// </summary>
        /// <value>
        /// <c>true</c> if this instance is default; otherwise, <c>false</c>.
        /// </value>
        public bool IsDefault { get; private set; }

        /// <summary>
        /// Gets the id.
        /// </summary>
        public int Id { get; private set; }

        /// <summary>
        /// Gets the name.
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// Gets the project id.
        /// </summary>
        public int ProjectId { get; private set; }

        /// <summary>
        /// Gets the query.
        /// </summary>
        public string Query { get; private set; }

        /// <summary>
        /// Gets the tickets count.
        /// </summary>
        public int TicketsCount { get; private set; }

        /// <summary>
        /// Gets the updated at.
        /// </summary>
        public DateTime UpdatedAt { get; private set; } 
        #endregion

        /// <summary>
        /// Parses the specified element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns>Parsed ticket bin</returns>
        /// <exception cref="ArgumentNullException">Element is null or empty.</exception>
        internal static TicketBin Parse(XElement element)
        {
            if (element == null || element.IsEmpty)
            {
                throw new ArgumentNullException("element");
            }

            bool isDefault;
// ReSharper disable PossibleNullReferenceException
            bool.TryParse(element.Element("default").Value, out isDefault);
            var id = int.Parse(element.Element("id").Value);
            var name = element.Element("name").Value;
            var projectId = int.Parse(element.Element("project-id").Value);
            var query = element.Element("query").Value;
            var ticketsCount = int.Parse(element.Element("tickets-count").Value);
            var updatedAt = Globals.DateTimeFromLighthouse(element.Element("updated-at").Value);
            var userId = int.Parse(element.Element("user-id").Value);
// ReSharper restore PossibleNullReferenceException

            return new TicketBin(
                isDefault, 
                id,
                name,
                projectId,
                query, 
                ticketsCount, 
                updatedAt,
                userId);
        }
    }
}
